package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.DataCenterService;
import com.yixun.qfbao.apidto.GetDevPremisesExportReqDto;
import com.yixun.qfbao.apidto.GetDevPremisesExportResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.DevPremisesExport;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.CalculateUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetDevPremisesExportReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 导出推荐人数据报表
 *
 * @classname: ExportReportRecommendHander.java
 * @author: Beau
 * @create: 2020/01/02 16:48
 * @version: 1.0.0
 **/
@Service("exportReportRecommendHander")
public class ExportReportRecommendHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(ExportReportRecommendHander.class);
    @Autowired
    private DataCenterService dataCenterService;
    @Autowired
    private LoginUtils loginUtils;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetDevPremisesExportReqVo reqDto1 = (GetDevPremisesExportReqVo) reqDto;
        GetDevPremisesExportReqDto dto = reqDto1.getParams();
        BaseResponseDTO<GetDevPremisesExportResDto> res = new BaseResponseDTO<>();
        GetDevPremisesExportResDto resDto = new GetDevPremisesExportResDto();
        PartnerInfo pi = loginUtils.getUser(dto.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        try {
            dto.setDevCompanyId(pi.getHigherid());
            List<DevPremisesExport> devPremisesExport = dataCenterService.exportDevPremises(dto);
            List<DevPremisesExport> collect = devPremisesExport.stream().filter(devPremisesReport -> {
                String signRate = CalculateUtil.signRate(devPremisesReport.getCustomerSuccessCount() + "", devPremisesReport.getSignCount() + "");
                devPremisesReport.setSignRate(signRate);
                return true;
            }).collect(Collectors.toList());
            resDto.setDevPremisesExportList(collect);
            resDto.setStartDate(dto.getStartDate());
            if (StringUtils.isEmpty(dto.getEndDate())) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String endDate = format.format(new Date());
                resDto.setExpirationDate(endDate);
            } else {
                resDto.setExpirationDate(dto.getEndDate());
            }
            res.setResData(resDto);
            res.setResult(ResultEnum.SUCCESS.getNo());
            return res;
        } catch (Exception e) {
            logger.error("ExportReportRecommendHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("导出数据报表失败");
            return res;
        }
    }
}
